<?php
/**
 * @version $Id: controller_OLD_copy.php,v 1.1 2009/06/03 00:34:12 dgravato Exp $
 * @package    sketchup
 * @subpackage 
 * @author     Chris Borkowski {@link http://www.guggenheim.org}
 * @author     Created on 06-May-2009
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.controller' );

class SketchupController extends JController 
{ 
	function __construct( $default = array() )
	{ 
		parent::__construct( $default ); 
		$this->registerTask( 'add' , 'edit' ); 
		$this->registerTask( 'apply', 'save' );
		$this->registerTask( 'unpublish', 'publish' );
	}

	function edit() 
	{ 
		global $option; 

		$row =& JTable::getInstance('sketchup', 'Table'); 
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' ); 
		$id = $cid[0]; 
		$row->load($id); 
		$lists = array(); 
		$lists['finalist'] = JHTML::_('select.booleanlist', 'finallist', 'class="inputbox"', $row->finalist); 
		$lists['published'] = JHTML::_('select.booleanlist', 'published', 'class="inputbox"', $row->published); 

		HTML_sketchups::editSketchup($row, $lists, $option); 
	}
 
	function save() 
	{ 
		global $option; 
		
		JRequest::checkToken() or die( 'Invalid Token' );
		
		$row =& JTable::getInstance('sketchup', 'Table');
	
		if (!$row->bind(JRequest::get('post'))) { 
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n"; 
			exit(); 
		}
		
		//for the description editor
		$row->p_description = JRequest::getVar( 'p_description', '', 'post', 'string', JREQUEST_ALLOWRAW );
		$row->id = (int) $row->id;
		$row->finalist = (int) $row->finalist;

		
		$date =& JFactory::getDate($row->sub_date);
		$row->sub_date = $date->toMySQL();
		
		if (!$row->store()) { 
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n"; 
			exit(); 
		}
		
		switch ($this->_task)        
		{
			case 'apply': 
				$msg = 'Changes to Sketchup saved'; 
				$link = 'index.php?option=' . $option . '&task=edit&cid[]='. $row->id; 
				break;
			
			case 'save': 
			default: 
				$msg = 'Sketchup Saved'; 
				$link = 'index.php?option=' . $option; 
				break; 
		}
		 
		$this->setRedirect($link, $msg); 
	} 

	function showSketchups() 
	{ 
		global $option, $mainframe;
		
		$limit = JRequest::getVar('limit', $mainframe->getCfg('list_limit')); 
		$limitstart = JRequest::getVar('limitstart', 0); 
		
		$db =& JFactory::getDBO(); 
		$query = "SELECT count(*) FROM #__sketchup"; 
		$db->setQuery( $query ); 
		$total = $db->loadResult(); 
		
		$query = "SELECT * FROM #__sketchup"; 
		$db->setQuery( $query, $limitstart, $limit ); 
		$rows = $db->loadObjectList(); 
		
		if ($db->getErrorNum()) { 
			echo $db->stderr(); 
			return false; 
		} 
		
		jimport('joomla.html.pagination'); 
		
		$pageNav = new JPagination($total, $limitstart, $limit); 
		
		HTML_sketchups::showSketchups( $option, $rows, $pageNav );
	} 

	function remove() 
	{ 
		global $option; 
		$cid = JRequest::getVar( 'cid', array(), '', 'array' ); 
		$db =& JFactory::getDBO();
		 
		if(count($cid)) 
		{ 
			JArrayHelper::toInteger($cid);
			$cids = implode( ',', $cid ); 
			$query = "DELETE FROM #__sketchup WHERE id IN ( $cids )"; 
			$db->setQuery( $query );
			if (!$db->query()) { 
				echo "<script> alert('".$db->getErrorMsg()."'); window. history.go(-1); </script>\n"; 
			} 
		}
		
		$this->setRedirect( 'index.php?option=' . $option ); 
	}
	
	function publish() 
	{ 
		global $option;

		$cid = JRequest::getVar( 'cid', array(), '', 'array' ); 
		
		if( $this->_task == 'publish') 
		{ 
			$publish = 1; 
		} 
		else 
		{ 
			$publish = 0; 
		}
		
		$SketchupTable =& JTable::getInstance('sketchup', 'Table'); 
		$SketchupTable->publish($cid, $publish); 
		$this->setRedirect( 'index.php?option=' . $option ); 
	}
	
	function comments() 
	{ 
		global $option, $mainframe;
		
		$limit = JRequest::getVar('limit', $mainframe->getCfg('list_limit'));
		$limitstart = JRequest::getVar('limitstart', 0);
		
		$db =& JFactory::getDBO(); 
		$query = "SELECT count(*) FROM #__sketchup_comments"; 
		$db->setQuery( $query ); 
		$total = $db->loadResult(); 
		
		$query = "SELECT c.*, r.p_title FROM #__sketchup_comments AS c LEFT JOIN #__sketchup AS r ON r.id = c.sketchup_id "; 
		$db->setQuery( $query, $limitstart, $limit ); 
		$rows = $db->loadObjectList(); 
		
		if ($db->getErrorNum()) 
		{ 
			echo $db->stderr(); 
			return false; 
		}
		
		jimport('joomla.html.pagination'); 
		$pageNav = new JPagination($total, $limitstart, $limit); 
		HTML_sketchups::showComments( $option, $rows, $pageNav ); 
	} 
	
	function editComment() 
	{ 
		global $option; 

		$row =& JTable::getInstance('comment', 'Table'); 
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' ); 
		$id = $cid[0]; 
		$row->load($id); 
		HTML_sketchups::editComment($row, $option); 
	}
	
	function saveComment() 
	{ 
		global $option; 

		$row =& JTable::getInstance('comment', 'Table'); 
		$row->comment_text = JRequest::getVar( 'comment_text', '', 'post', 'string', JREQUEST_ALLOWRAW );
		
		if (!$row->bind(JRequest::get('post'))) { 
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n"; 
			exit(); 
		}
		
		if (!$row->store()) { 
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n"; 
			exit(); 
		} 
		
		$this->setRedirect('index.php?option=' . $option . '&task=comments', 'Comment changes saved'); 
	}
	
	function removeComment() 
	{ 
		global $option; 
		$cid = JRequest::getVar( 'cid', array(), '', 'array' ); 
		$db =& JFactory::getDBO(); 

		if(count($cid)) 
		{ 
			JArrayHelper::toInteger($cid);
			$cids = implode( ',', $cid ); 
			$query = "DELETE FROM #__sketchup_comments WHERE id IN ( $cids )"; 
			$db->setQuery( $query ); 
			
			if (!$db->query()) { 
				echo "<script> alert('".$db->getErrorMsg()."'); window.history.go(-1); </script>\n"; 
			} 
		} 
		
		$this->setRedirect( 'index.php?option=' . $option . '&task=comments' ); 
	}
	
	function thumbs() 
	{ 
		global $option, $mainframe;
		
		$limit = JRequest::getVar('limit', $mainframe->getCfg('list_limit')); 
		$limitstart = JRequest::getVar('limitstart', 0); 
		
		$db =& JFactory::getDBO(); 
		$query = "SELECT count(*) FROM #__sketchup"; 
		$db->setQuery( $query ); 
		$total = $db->loadResult(); 
		//make more granular to check all rows
		$query = "SELECT * FROM #__sketchup"; 
		$db->setQuery( $query, $limitstart, $limit ); 
		$rows = $db->loadObjectList(); 
		
		if ($db->getErrorNum()) { 
			echo $db->stderr(); 
			return false; 
		} 
		
		jimport('joomla.html.pagination'); 
		
		$pageNav = new JPagination($total, $limitstart, $limit); 
		
		HTML_sketchups::showSketchupsThumbs( $option, $rows, $pageNav );
	} 
	
		function makethumbs()
		{
		
		global $option, $mainframe;
		
		//First we'll set up parameters, make these global to the component soon
		jimport('joomla.filesystem.file');
		
		$uploads_path = JPATH_ROOT . DS . "images". DS ."content" . DS ."sketchup". DS . "uploads" ;

		$tmp_path = JPATH_ROOT . DS . "images". DS ."content" . DS ."sketchup". DS . "tmp" ;
		
		/* Set some vars
		 * this is jenky
		 * @ var mixed
		 * access public
		*/

		$new_w = 100;

		$cfg_fullsizepics_path = $uploads_path;

		$cfg_thumb_path = $tmp_path;

		$filepath = $uploads_path ;

		$dir = dir($filepath); 

			//check folders
			
			while($entry=$dir->read()) { 

    		if($entry == "." || $entry == "..") { 

        	continue; 
			} 
			
			$fp = @fopen("$filepath/$entry","r");

			$photo_filename = "$entry";

			$image_stats = GetImageSize($cfg_fullsizepics_path."/".$entry);

			$imagewidth = $image_stats[0];

			$imageheight = $image_stats[1];

			$img_type = $image_stats[2];

			$ratio = ($imagewidth / $new_w);

			$new_h = round($imageheight / $ratio);

			if (!file_exists($cfg_thumb_path."/".$entry))  {

				if ($img_type=="2") {

					$src_img = imagecreatefromjpeg($cfg_fullsizepics_path."/".$entry);

					$dst_img = imagecreate($new_w,$new_h);

					imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));

					imagejpeg($dst_img, "$cfg_thumb_path"."/$entry", 100);

 				} elseif  ($img_type=="3") {

 					$dst_img=ImageCreate($new_w,$new_h);

 					$src_img=ImageCreateFrompng($cfg_fullsizepics_path."/".$entry);

					ImageCopyResized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));

 					Imagepng($dst_img, "$cfg_thumb_path"."/$entry",100);

 				} elseif  ($img_type=="1") {

 					$cfg_thumb_url=$cfg_fullsizepics_url;

 				}

			}

				
  				
  		} //  now lets move them

		

		$searchpath = JPATH_ROOT . DS . "images". DS ."content" . DS ."sketchup". DS . "tmp" ;
		$destpath = JPATH_ROOT . DS . "images". DS ."content" . DS ."sketchup". DS . "thumbs" ;
  
		//read all the image files and put them in an array.
		$img_files = JFolder::files($searchpath, '.*');
 
		//Now we need some stuff from the JFile:: class to move all files into the new folder
		foreach ($img_files as $file) {
   		JFile::move($searchpath. DS . $file, $destpath . DS. $file);
		}
 
		//report the good news.
		switch ($this->_task){
		case 'movieimages': 
		$msg = 'Thumbs were moved';
		break;
		}
		
		$this->setRedirect( 'index.php?option=' . $option . '&task=thumbs', 'Thumbnails made' );
      

  			

} //the end thumbnail creation



} //this is the end of the controller
?>